const request = require('../../utils/request.js');
Page({

  data: {
    banners: [],
    stores: [],
    storeNames: [],
    isModalVisible: false,
    selectedStore: '',
    selectedStoreId:null,
    selectedTime: '',
    showStoreInfo: false,
  },
  onLoad(){
    this.finimage();
    this.findianpu();
  },
  preventClose() {
    // 阻止点击弹出框区域关闭
    this.setData({
      selectedDateTime: ''
    });
  },
  onDateTimeChange(e) {
    this.setData({
      selectedDateTime: e.detail.value
    });
  },
  openOrderModal() {
    this.setData({
      isModalVisible: true
    });
  },

  closeModal() {
    this.setData({
      isModalVisible: false
    });
  },

  onStoreChange(e) {
    console.log('点击');
    this.setData({
      showStoreInfo: true // 弹出店铺信息
    });
  },
  onCloseStoreInfo() {
    this.setData({
      showStoreInfo: false
    });
  },

  onTimeChange(e) {
    this.setData({
      selectedTime: e.detail.value
    });
  },

  enterStore: function(event) {
    // 获取按钮上的 data-id 值
    const storeId = event.currentTarget.dataset.id;
    console.log('进入店铺:', storeId);

    // 使用 wx.navigateTo 跳转到店铺详情页，带上 storeId 参数
    wx.navigateTo({
      url: `/pages/store/store?storeId=${storeId}`,
    });
  },

  finimage(){
    request.post("/index/finimg")
    .then(res=>{
      this.setData({
        banners: res.data // 使用 setData 更新页面数据
      });
    }).catch(res=>{
    })
  },

  findianpu(){
    request.post("/index/findianpu")
    .then(res=>{
      console.log(res);
      this.setData({
        stores: res.data,
        storeNames: res.data.map(store => store.name)
      });
      console.log(this.stores)
    }).catch(res=>{
    })
  },

  xiadan(){
    const formattedDate = new Date(this.data.selectedTime).toISOString();
    const param  = {
      merchantId:this.data.selectedStoreId,
      appointmentTime:formattedDate
    }
    request.post("/index/addxiyi",param)
    .then(res =>{
      console.log(res);
      if (res.code === 0) {
        wx.showToast({ title: res.msg, icon: 'none' });
        this.closeModal();
      }
    })
  },

  selectStore(e) {
    const index = e.currentTarget.dataset.index;
    const store = this.data.stores[index];
    this.setData({
      selectedStore: store.name,
      selectedStoreId: store.id,
      showStoreInfo: false, // 关闭弹出层
    });
  }
  
});
